<?php

namespace App\Admin\Metrics;


use App\Models\User;
use App\Services\TimeService;
use Carbon\Carbon;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;

class ExpertTrend extends Line
{
    // 专家近七天趋势
    /**
     * 图表默认高度.
     *
     * @var int
     */
    protected $chartHeight = 53;
    protected $height = 53;
    protected $chartMarginRight = 1;

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $seven = [
            Carbon::today()->subDays(6)->toDateTimeString(),
            Carbon::now()->startOfDay()->toDateTimeString(),
        ];

        $weekDetail = TimeService::prDates($seven[0], $seven[1]);

        $data = [];

        foreach ($weekDetail as $key => $val) {
            $count = User::whereDate('created_at', $val)->count();
            array_push($data, $count);
        }

        $year_all = array_sum($data);

        $this->withContent('数量：' . $year_all);
        // 图表数据
        $this->withChart($data);
    }

    /**
     * 设置卡片内容.
     *
     * @param string $content
     *
     * @return WorthTrend
     */
    public function withContent(string $content): ExpertTrend
    {
        return $this->content(
            <<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
    <h4 class="ml-1">{$content}</h4>
</div>
HTML
        );
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return WorthTrend
     */
    public function withChart(array $data): ExpertTrend
    {
        $this->chartOptions['tooltip']['x']['show'] = false;

        return $this->chart([
            'series' => [
                [
                    'name' => '近七天',
                    'data' => $data,
                ],
            ],
            'tooltip' => [
                'x' => [
                    'show' => true,
                ],
                'y' => [
                    'show' => true,
                ],
            ],
        ]);
    }

    /**
     * 初始化卡片内容.
     *
     * @return void
     */
    protected function init()
    {
        parent::init();

        $this->title('近七天专家趋势');
        // $this->height(250);
        $seven = [
            Carbon::now()->subDays(7)->toDateTimeString(),
            Carbon::now()->startOfDay()->toDateTimeString(),
        ];

        $weekDetail = TimeService::prDates($seven[0], $seven[1]);
        // dd($weekDetail);
        $this->chartLabels($weekDetail);
        // $this->dropdown([
        //     'current_year' => trans('main.current_year'),
        //     'pre_year' => trans('main.last_year'),
        // ]);
    }
}
